import { Button, Form, Popconfirm } from 'antd';
import { messages } from 'share/common';
import React from 'react';
import { columnsMap, levelMap, formMap, searchColumnsMap } from './dataSources';

export function tableDecorator(target) {
  return class extends target {

    handleLevelMessage = (flag) => levelMap.has(flag) ? levelMap.get(flag) : {};

    handleColumns = (columns) => columns.map(
      item => {
        const middleItem = columnsMap.get(item);
        if (middleItem.dataIndex === 'operation') {
          middleItem.render = (value, record) => (
            <Popconfirm
              onConfirm={() => this.handleDelete(record)}
              title={messages('expense-3.key109')/*确认删除？*/}
            >
              <Button
                disabled={record.readOnly}
                ghost
                type="link"
                onClick={(e) => e.stopPropagation()}
              >
                {messages('common.delete')/*删除*/}
              </Button>
            </Popconfirm>
          );
        }
        if (middleItem.dataIndex === 'operationSpecial') {
          middleItem.width = '15%';
          middleItem.render = (value, record) => !record.defaultUsage
            ? (
              <Popconfirm
                onConfirm={() => this.handleRestore(record)}
                title={messages('expense-3.key110')/*恢复默认设置？*/}
              >
                <Button
                  disabled={record.readOnly}
                  ghost
                  type="link"
                  onClick={(e) => e.stopPropagation()}
                >
                  {messages('expense-3.key111')/*恢复默认设置*/}
                </Button>
              </Popconfirm>
            )
            : '-';
        }
        return middleItem;
      });

    handleSearchForm = (searchColumns) => searchColumns.map(item => searchColumnsMap.get(item));
  };
}

export function formDecorator(target) {
  return class extends target {

    formItemLayout = {
      labelCol: { span: 4 },
      wrapperCol: { span: 12, offset: 1 }
    };

    isNull = () => {
      const { params: { key } } = this.props;
      const objFunc = {
        'head-up-verification': (item) => {
          const { form: { getFieldValue } } = this.props;
          if (!getFieldValue('invoiceTitleValidate') && ['receiptTypeList', 'titleValidateDetail', 'titleValidateMode', 'titleValidateRange'].includes(item)) return true;
          return !(getFieldValue('expenseReportTitleValidate') || getFieldValue('invoiceTitleValidate')) && ['noInvoiceTitleValidate'].includes(item);
        },

        'consecutive-verification': (item) => {
          const { form: { getFieldValue } } = this.props;
          return !getFieldValue('receiptConsecutiveControl') && ['expenseTypeList', 'receiptTypeList', 'consecutiveAbsolute', 'consecutiveCheck'].includes(item);
        }
      };
      return objFunc[key];
    };

    handleLevelMessage = (flag) => levelMap.has(flag) ? levelMap.get(flag) : {};

    renderFormList = () => {
      const { formList } = this.state;
      const { form: { getFieldDecorator }, params: { readOnly, key }, id } = this.props;
      return Object
        .keys(formList)
        .map(item => {
          let middleItem = item;
          if (formList[item].isRegex) middleItem = item.replace(/\d/g, '');
          if (!formMap.has(middleItem)) return null;
          const middleForm = formMap.get(middleItem);
          const { name, child, params = {}, component, isNotForm } = middleForm;
          const { required, extra, initData, valuePropName } = formList[item];
          if (isNotForm){
            let notFormParams = formList[item].params || {};
            notFormParams.key = item;
            (middleItem === 'overdueTable' || middleItem === 'complianceRuleCard') && (notFormParams.id = id);
            middleItem === 'jumpConfig' && (notFormParams = { tab: key, ...notFormParams });
            return React.createElement(component, notFormParams, formList[item].child);
          }
          if (key && this.isNull() && this.isNull()(item)) return null;
          !params.disabled && (params.disabled = readOnly);
          params.key = item;
          return (
            <Form.Item extra={extra} label={name} key={item}>
              {
                getFieldDecorator(item, {
                  rules: [{
                    required: !!required,
                    message: messages('expense-3.key78')/*请输入temp2*/ + name
                  }],
                  initialValue: initData,
                  valuePropName: valuePropName || 'value'
                })(React.createElement(component, params, child))
              }
            </Form.Item>
          );
        });
    };
  };
}
